package ch.unisi.inf.editor;

import java.awt.Color;
import java.util.ArrayList;
import ch.unisi.inf.engine.Image;
/**
 * this filter replaces the color value of a pixel with the difference between the max and the min color-value of its 8 neighbors
 * 
 * @author matthias.eberli@lu.unisi.ch 
 */
public class EdgeFilter extends Filter
{
    private Image tempImage;
    
    public EdgeFilter(final String name)
    {
        super(name);
    }
    
    public void apply(final Image image)
    {
        tempImage = new Image(image.getPath(), image);
        int height = image.getHeight();
        int width = image.getWidth();
        for(int y = 0; y < height; y++) {
            for(int x = 0; x < width; x++) {
                Color pixel = getDifference(tempImage, x, y);
                image.setPixel(x, y, pixel);
            }
        }
    }   
    
    /**
     * returns the color of the pixel if the pixel is inside the image
     * else it returns null
     * @param image the image on which it gets the pixel
     * @param x the x position of the pixel
     * @param y the y position of the pixel
     */
    public Color getExistingPixel(final Image image, final int x, final int y)
    {
        int height = image.getHeight();
        int width = image.getWidth();
        if (x >= width || x < 0 || y >= height || y < 0){
            return null;
        }
        Color pixel = image.getPixel(x,y);
        return pixel;
    }
    
    /**
     * returns a new color object with as RGB value the difference of the max and min of the neighbors
     * @param image the image where the pixels are taken from
     * @param x the x position of the pixel
     * @param y the y position of the pixel
     */
    private Color getDifference(final Image image, final int x, final int y)
    {
        ArrayList<Color> list = new ArrayList<Color>();
        list.add(getExistingPixel(image, x,y));
        list.add(getExistingPixel(image, x,y+1));
        list.add(getExistingPixel(image, x,y-1));
        list.add(getExistingPixel(image, x+1,y));
        list.add(getExistingPixel(image, x+1,y+1));
        list.add(getExistingPixel(image, x+1,y-1));
        list.add(getExistingPixel(image, x-1,y));
        list.add(getExistingPixel(image, x-1,y+1));
        list.add(getExistingPixel(image, x-1,y-1));
        
        int minRed = 255;
        int maxRed = 0;
        int minGreen = 255;
        int maxGreen = 0;
        int minBlue = 255;
        int maxBlue = 0;
        
        for(final Color color : list){
            if(color != null){
                if(color.getRed() < minRed){
                    minRed = color.getRed();
                }
                if(color.getRed() >= minRed){
                    maxRed=color.getRed();
                }
                if(color.getGreen() < minGreen){
                    minGreen = color.getGreen();
                }
                if(color.getGreen() >= maxGreen){
                    maxGreen = color.getGreen();
                }
                if(color.getBlue() < minBlue){
                    minBlue = color.getBlue();
                }
                if(color.getBlue() >= maxBlue){
                    maxBlue = color.getBlue();
                }
            }
        }
        return new Color(maxRed - minRed, maxGreen - minGreen, maxBlue - minBlue);
    }
}
